import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建MySQL连接引擎
engine = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/rent_xian')

# 创建会话工厂
Session = sessionmaker(bind=engine)

# 创建基类
Base = sqlalchemy.orm.declarative_base()

# 定义模型类
class House(Base):
    __tablename__ = 'house_data'
    id = Column(Integer, primary_key=True)
    area = Column(String(100))
    located = Column(String(100))
    address = Column(String(200))
    type = Column(String(20))
    floor_area = Column(Float)
    room_orientation = Column(String(100))
    house_type = Column(String(20))
    height = Column(String(20))
    price = Column(Integer)
    labels = Column(String(200))

    def __repr__(self):
        return '<House %r>' % self.address


# 创建会话实例
session = Session()

# 插入数据
data = [
    ('新城区', '城东', '黄河机器制造厂十街坊小区', '整租', 45.00, '南 北', '2室0厅1卫', '6层', 1400, '精装,集中供暖,随时看房')
]

for d in data:
    house = House(area=d[0], located=d[1], address=d[2], type=d[3], floor_area=d[4], room_orientation=d[5],
                  house_type=d[6], height=d[7], price=d[8], labels=d[9])
    session.add(house)

# 提交事务
session.commit()

# 关闭会话
session.close()
